package stu.day19_day24.day21;

import java.util.Arrays;

public class LeetCode1011 {
    public int shipWithinDays(int[] weights, int days) {

        int left = Arrays.stream(weights).max().getAsInt();
        int right = Arrays.stream(weights).sum()+1;
        int mid;
        int sum;
        while (left<right){
            mid = (left+right)/2;
            sum = all_sum(mid,weights);
            if(sum<=days){
                right = mid;
            }else {
                left = mid+1;
            }
        }
        return left;

    }


    public int all_sum(int mid,int[] weights){
        int sum = 0;
        int all = 0;
        for(int i : weights){
            all += i;
            if(all<=mid){
                continue;
            }else if(all>mid){
                sum += 1;
                all = i;
            }
        }

        if(all!=0){
            sum += 1;
        }

        return sum;
    }


}
